import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'

const request = axios.create({
  baseURL: '/api',
  timeout: 200000
})

//请求拦截器
request.interceptors.request.use((config)=> {

  let userTempId = store.state.user.userTempId
  if (userTempId) {
    config.headers.userTempId = userTempId
  }

  //请求头携带token
  let token = store.state.user.token
  if(token) {
    config.headers.token = token
  }

  NProgress.start()

  return config
},(err) => {
  NProgress.done()
  console.error(err);
  return Promise.reject(err)
}
)

//响应拦截器
request.interceptors.response.use((response) => {

  NProgress.done()

  // console.log(response);

  return response.data
},(err) => {
  NProgress.done()
  console.error(err);
  return Promise.reject(err)
})

export default request;